package com.house.core.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.JdbcUtils;

/**
 * 
 * @author SunChong
 */
public class DBManagerSpringImpl implements DBManager {
	private JdbcTemplate jdbcTemplate;
	public DBManagerSpringImpl(){
		
	}
	public DBManagerSpringImpl(JdbcTemplate jdbcTemplate){
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public Object queryUniq(OpQueryUniq op) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = this.getJdbcTemplate().getDataSource().getConnection();
			ps = con.prepareStatement(op.getSql());
			op.setParam(ps);
			rs = ps.executeQuery();
			if(rs.next()){
				return op.parse(rs);
			}
			return null;
		} catch (SQLException e) {
			throw e;
		} finally {
			JdbcUtils.closeResultSet(rs);
			JdbcUtils.closeStatement(ps);
			JdbcUtils.closeConnection(con);
		}
	}

	@Override
	public List<Object> queryList(OpQueryList op) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = this.getJdbcTemplate().getDataSource().getConnection();
			ps = con.prepareStatement(op.getSql());
			op.setParam(ps);
			rs = ps.executeQuery();
			List<Object> list = new ArrayList<Object>();
			while(rs.next()){
				list.add(op.parse(rs));
			}
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			JdbcUtils.closeResultSet(rs);
			JdbcUtils.closeStatement(ps);
			JdbcUtils.closeConnection(con);
		}
	}
	@Override
	public int update(OpUpdate op) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = this.getJdbcTemplate().getDataSource().getConnection();
			ps = con.prepareStatement(op.getSql());
			op.setParam(ps);
			return ps.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			JdbcUtils.closeResultSet(rs);
			JdbcUtils.closeStatement(ps);
			JdbcUtils.closeConnection(con);
		}
	}
	@Override
	public int insertReturnIntId(OpUpdate op) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = this.getJdbcTemplate().getDataSource().getConnection();
			ps = con.prepareStatement(op.getSql(), Statement.RETURN_GENERATED_KEYS);
			op.setParam(ps);
			ps.executeUpdate();
			rs = ps.getGeneratedKeys();
            if (rs.next()){
                return rs.getInt(1);
            }
		} catch (SQLException e) {
			throw e;
		} finally {
			JdbcUtils.closeResultSet(rs);
			JdbcUtils.closeStatement(ps);
			JdbcUtils.closeConnection(con);
		}
		return 0;
	}
	@Override
	public long insertReturnLongId(OpUpdate op) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = this.getJdbcTemplate().getDataSource().getConnection();
			ps = con.prepareStatement(op.getSql(), Statement.RETURN_GENERATED_KEYS);
			op.setParam(ps);
			ps.executeUpdate();
			rs = ps.getGeneratedKeys();
            if (rs.next()){
                return rs.getLong(1);
            }
		} catch (SQLException e) {
			throw e;
		} finally {
			JdbcUtils.closeResultSet(rs);
			JdbcUtils.closeStatement(ps);
			JdbcUtils.closeConnection(con);
		}
		return 0;
	}
	@Override
	public boolean queryExist(OpQueryUniq op) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = this.getJdbcTemplate().getDataSource().getConnection();
			ps = con.prepareStatement(op.getSql());
			op.setParam(ps);
			rs = ps.executeQuery();
			if(rs.next()){
				return true;
			}
			return false;
		} catch (SQLException e) {
			throw e;
		} finally {
			JdbcUtils.closeResultSet(rs);
			JdbcUtils.closeStatement(ps);
			JdbcUtils.closeConnection(con);
		}
	}
}
